package com.fr.report.script.core.parser;

import com.fr.report.script.Calculator;
import com.fr.util.Utils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MultiExpression extends MathExpression
{
  private static final String MULTI = "*";
  private static final String DIV = "/";
  private static final String MOD = "%";
  private static final int DEFAULT_DECIMAL_SCALE = 20;
  String[] ops;
  PowerExpression[] powers;

  MultiExpression(PowerExpression[] paramArrayOfPowerExpression, String[] paramArrayOfString)
  {
    this.powers = paramArrayOfPowerExpression;
    this.ops = paramArrayOfString;
  }

  private static boolean isMULTI(String paramString)
  {
    return "*".equals(paramString);
  }

  private static boolean isDIV(String paramString)
  {
    return "/".equals(paramString);
  }

  private static boolean isMOD(String paramString)
  {
    return "%".equals(paramString);
  }

  public Object eval(Calculator paramCalculator)
    throws UtilEvalError
  {
    if (this.powers.length == 0)
      throw new InterpreterError("powerList should not be empty");
    Object localObject1 = null;
    for (int i = 0; i < this.powers.length; ++i)
      if (i == 0)
      {
        localObject1 = this.powers[i].eval(paramCalculator);
      }
      else
      {
        Object localObject2 = this.powers[i].eval(paramCalculator);
        localObject1 = binaryOperation(localObject1, localObject2, this.ops[(i - 1)]);
      }
    return localObject1;
  }

  protected Object ccNINI(String paramString)
  {
    if (isMOD(paramString))
      return OperationUtils.ZERO;
    if (isMULTI(paramString))
      return OperationUtils.POSITIVE_INFINITY;
    return new Integer(1);
  }

  protected Object ccPIPI(String paramString)
  {
    return ccNINI(paramString);
  }

  protected Object ccNIPI(String paramString)
  {
    if (isMOD(paramString))
      return OperationUtils.ZERO;
    if (isMULTI(paramString))
      return OperationUtils.NEGATIVE_INFINITY;
    return new Integer(-1);
  }

  protected Object ccPINI(String paramString)
  {
    return ccNIPI(paramString);
  }

  protected Object ccNIRE(Object paramObject, String paramString)
  {
    if (isMOD(paramString))
      return OperationUtils.ZERO;
    Number localNumber = Utils.objectToNumber(paramObject, true);
    if (localNumber == null)
      return OperationUtils.NEGATIVE_INFINITY;
    int i = localNumber.intValue();
    if (isMULTI(paramString))
    {
      if (i > 0)
        return OperationUtils.NEGATIVE_INFINITY;
      if (i == 0)
        return OperationUtils.ZERO;
      return OperationUtils.POSITIVE_INFINITY;
    }
    if (i >= 0)
      return OperationUtils.NEGATIVE_INFINITY;
    return OperationUtils.POSITIVE_INFINITY;
  }

  protected Object ccPIRE(Object paramObject, String paramString)
  {
    Object localObject = ccNIRE(paramObject, paramString);
    if (localObject == OperationUtils.POSITIVE_INFINITY)
      return OperationUtils.NEGATIVE_INFINITY;
    if (localObject == OperationUtils.NEGATIVE_INFINITY)
      return OperationUtils.POSITIVE_INFINITY;
    return localObject;
  }

  protected Object ccRENI(Object paramObject, String paramString)
  {
    if ((isMOD(paramString)) || (isDIV(paramString)))
      return OperationUtils.ZERO;
    Number localNumber = Utils.objectToNumber(paramObject, true);
    if (localNumber == null)
      return OperationUtils.NEGATIVE_INFINITY;
    int i = localNumber.intValue();
    if (i > 0)
      return OperationUtils.NEGATIVE_INFINITY;
    if (i == 0)
      return OperationUtils.ZERO;
    return OperationUtils.POSITIVE_INFINITY;
  }

  protected Object ccREPI(Object paramObject, String paramString)
  {
    Object localObject = ccRENI(paramObject, paramString);
    if (localObject == OperationUtils.POSITIVE_INFINITY)
      return OperationUtils.NEGATIVE_INFINITY;
    if (localObject == OperationUtils.NEGATIVE_INFINITY)
      return OperationUtils.POSITIVE_INFINITY;
    return localObject;
  }

  protected Object ccRERE(Object paramObject1, Object paramObject2, String paramString)
  {
    return numberOperation(paramObject1, paramObject2, paramString);
  }

  protected Object decimalOperation(BigDecimal paramBigDecimal1, BigDecimal paramBigDecimal2, String paramString)
  {
    if (isMULTI(paramString))
      return paramBigDecimal1.multiply(paramBigDecimal2);
    if (isDIV(paramString))
    {
      if (paramBigDecimal1.compareTo(OperationUtils.BZERO) == 0)
        return new Integer(0);
      if (paramBigDecimal2.compareTo(OperationUtils.BZERO) == 0)
        return ((paramBigDecimal1.compareTo(OperationUtils.BZERO) > 0) ? OperationUtils.POSITIVE_INFINITY : OperationUtils.NEGATIVE_INFINITY);
      return paramBigDecimal1.divide(paramBigDecimal2, (paramBigDecimal1.scale() > 20) ? paramBigDecimal1.scale() : 20, 6);
    }
    return paramBigDecimal1.toBigInteger().mod(paramBigDecimal2.toBigInteger());
  }

  protected Object doubleOperation(double paramDouble1, double paramDouble2, String paramString)
  {
    if (isMULTI(paramString))
      return new Double(paramDouble1 * paramDouble2);
    if (isDIV(paramString))
    {
      if (paramDouble1 == 0.0D)
        return new Integer(0);
      if (paramDouble2 == 0.0D)
        return ((paramDouble1 > 0.0D) ? OperationUtils.POSITIVE_INFINITY : OperationUtils.NEGATIVE_INFINITY);
      return new Double(paramDouble1 / paramDouble2);
    }
    return new Double(paramDouble1 % paramDouble2);
  }

  protected Object intOperation(int paramInt1, int paramInt2, String paramString)
  {
    if (isMULTI(paramString))
      return new Integer(paramInt1 * paramInt2);
    if (isDIV(paramString))
    {
      if (paramInt1 == 0)
        return new Integer(0);
      if (paramInt2 == 0)
        return ((paramInt1 > 0) ? OperationUtils.POSITIVE_INFINITY : OperationUtils.NEGATIVE_INFINITY);
      return new Double(paramInt1 / paramInt2);
    }
    return new Integer(paramInt1 % paramInt2);
  }

  public void traversal4Tiny(TinyHunter paramTinyHunter)
  {
    for (int i = 0; i < this.powers.length; ++i)
      this.powers[i].traversal4Tiny(paramTinyHunter);
  }

  public String exString(Calculator paramCalculator)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.powers.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append(' ').append(this.ops[(i - 1)]).append(' ');
      localStringBuffer.append(this.powers[i].exString(paramCalculator));
    }
    return localStringBuffer.toString();
  }

  public String toString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.powers.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append(' ').append(this.ops[(i - 1)]).append(' ');
      localStringBuffer.append(this.powers[i]);
    }
    return localStringBuffer.toString();
  }

  public String getExpression(int paramInt1, int paramInt2, int paramInt3, int paramInt4, boolean paramBoolean)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.powers.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append(' ').append(this.ops[(i - 1)]).append(' ');
      localStringBuffer.append(this.powers[i].getExpression(paramInt1, paramInt2, paramInt3, paramInt4, paramBoolean));
    }
    return localStringBuffer.toString();
  }

  public String[] parserParameter()
  {
    ArrayList localArrayList = new ArrayList();
    int i = 0;
    int j = this.powers.length;
    while (i < j)
    {
      localArrayList.addAll(Arrays.asList(this.powers[i].parserParameter()));
      ++i;
    }
    return ((String[])localArrayList.toArray(new String[localArrayList.size()]));
  }
}